package com.zj.healthmanage.controller;

import com.zj.healthmanage.constant.MessageConstant;
import com.zj.healthmanage.constant.RedisConstant;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.entity.Result;
import com.zj.healthmanage.pojo.Address;
import com.zj.healthmanage.service.AddressService;
import com.zj.healthmanage.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;


@RestController
@RequestMapping(value = "/address")
@Api(value = "体检机构地址",description = "体检机构地址")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private JedisPool jedisPool;

    @ApiOperation(value = "分页查询体检机构地址",httpMethod = "POST")
    @RequestMapping("queryAddressByPage")
    public PageResult queryAddressByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.queryAddressByPage(queryPageBean);
        return pageResult;
    }

    @ApiOperation(value = "上传图片",httpMethod = "POST")
    @RequestMapping("/upload")
    public Result uploadImage(@RequestParam("addressImg") MultipartFile addressImg){
        try {
            String filename = addressImg.getOriginalFilename();
            if (filename != null) {
                int index = filename.lastIndexOf(".");
                String imgFilename = UUID.randomUUID().toString() + filename.substring(index - 1);
                //upload到云上的文件名进行存储 --> redis
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,imgFilename);

                byte[] bytes = addressImg.getBytes();
                QiniuUtils.upload2Qiniu(bytes, imgFilename);
                return new Result(true, "图片上传成功",imgFilename);
            }else{
                return new Result(false,"图片上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @ApiOperation(value = "新增体检机构地址",httpMethod = "POST")
    @RequestMapping("addAddress")
    public Result addAddress(@RequestBody Address address){
        try {
            addressService.insertSelective(address);
            return  new Result(true,"新增机构地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增机构地址失败");
        }
    }

    @ApiOperation(value = "编辑体检机构地址",httpMethod = "POST")
    @RequestMapping("editAddress")
    public Result editAddress(@RequestBody Address address){
        try {
            addressService.updateByPrimaryKeySelective(address);
            return new Result(true,"修改机构地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改机构地址失败");
        }
    }

    @ApiOperation(value = "删除体检机构地址",httpMethod = "POST")
    @GetMapping("/deleteAddress")
    public Result deleteAddress(Integer id) {
        try {
            addressService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除检查机构失败");
        }
        return new Result(true,"删除检查机构成功");
    }

    @ApiOperation(value = "查询单个体检机构地址",httpMethod = "POST")
    @RequestMapping("queryAddressById")
    public Result queryAddressById(Integer id){
        try {
            Address address = addressService.queryAddressById(id);
            return new Result(true,"查询成功",address);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }
}
